import QtQuick
import Qt5Compat.GraphicalEffects

import Fk
import Fk.Widgets as W

Item {
  id: root

  property bool hovered: false
  property bool enabled: true
  property alias img: img
  property alias text: imgText
  signal clicked
  signal rightClicked

  width: Math.max(imgText.width + 10, 20)
  height: img.height + 2

  BorderImage {
    id: img
    source: Cpp.path + "/packages/lunarltk-qsgs-ui/image/selfArea/choiceBox/small-button.png"
    border.left: 5
    border.right: 5
    border.top: 5
    border.bottom: 5
    width: parent.width
    height: imgText.height + 5
  }

  Glow {
    id: textGlow
    source: imgText
    anchors.fill: imgText
    color: '#000000'
    spread: 0.2
    radius: 1
  }

  Text {
    id: imgText
    text: ""
    width: contentWidth
    height: contentHeight
    anchors.centerIn: img
    color: '#ffffff'
    font.pixelSize: 10
    font.bold: true
  }

  BrightnessContrast {
    anchors.fill: parent
    source: parent
    brightness: parent.hovered ? 0.3 : 0
    visible: root.enabled && parent.hovered
  }

  Desaturate {
    anchors.fill: parent
    source: parent
    desaturation: 1.0   // 0.0 = 原色，1.0 = 黑白
    visible: !root.enabled
  }

  W.TapHandler {
    id: mouse
    acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.NoButton
    gesturePolicy: TapHandler.WithinBounds

    onTapped: (p, btn) => {
      if (parent.enabled) {
        if (btn === Qt.LeftButton || btn === Qt.NoButton) {
          parent.clicked();
        } else if (btn === Qt.RightButton) {
          parent.rightClicked();
        }
      }
    }

    onLongPressed: {
      parent.rightClicked();
    }
  }

  HoverHandler {
    id: hover
    cursorShape: root.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
    onHoveredChanged: {
      if (hovered && root.enabled) {
        root.hovered = true;
      } else {
        root.hovered = false;
      }
    }
  }

}